<template>
  <div class="app-container">
    <div class="points-container">
      <div class="user-info-section">
        <div class="user-info-card">
          <div class="user-info">
            <div class="avatar-container">
              <el-avatar :size="180" :src="userInfo.avatar"></el-avatar>
            </div>
            <div class="info-container">
              <h2 class="username">{{ userInfo.userName }}</h2>
              <div class="info-item">
                <i class="el-icon-message"></i>
                <span>{{ userInfo.email }}</span>
              </div>
              <div class="info-item">
                <i class="el-icon-phone"></i>
                <span>{{ userInfo.phoneNumber }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="points-card">
          <div class="points-header">
            <i class="el-icon-money"></i>
            <span>我的积分</span>
          </div>
          <div class="points-value">{{ userInfo.points }}</div>
          <div class="points-desc">当前可用积分</div>
          <div class="points-actions">
            <el-button type="primary" size="large" @click="handleRecharge">充值积分</el-button>
            <el-button type="info" size="large" @click="handleViewDetails">查看积分明细</el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 充值积分对话框 -->
    <el-dialog title="积分充值" :visible.sync="rechargeOpen" width="700px" append-to-body custom-class="recharge-dialog">
      <el-form ref="rechargeForm" :model="rechargeForm" :rules="rechargeRules" label-width="100px">
        <el-form-item label="充值金额" prop="totalAmount">
          <el-radio-group v-model="rechargeForm.totalAmount" class="amount-group">
            <el-radio-button :label="10">10元</el-radio-button>
            <el-radio-button :label="20">20元</el-radio-button>
            <el-radio-button :label="30">30元</el-radio-button>
            <el-radio-button :label="50">50元</el-radio-button>
            <el-radio-button :label="100">100元</el-radio-button>
            <el-radio-button :label="200">200元</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="large" @click="submitRecharge" :loading="rechargeLoading">立即充值</el-button>
        <el-button size="large" @click="cancelRecharge">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 积分详情弹窗 -->
    <el-dialog title="积分明细" :visible.sync="detailsVisible" width="70%">
      <div class="points-details">
        <div v-for="(item, index) in pointsDetails" :key="index" class="points-detail-item">
          <div class="detail-left">
            <div class="detail-type">
              <el-tag :type="getTypeTag(item.type)">{{ getTypeText(item.type) }}</el-tag>
            </div>
            <div class="detail-info">
              <template v-if="item.type === 1">
                <div class="detail-title">积分充值</div>
              </template>
              <template v-else>
                <div class="detail-title">{{ item.novelName }}</div>
                <div class="detail-subtitle" v-if="item.orderNo">{{ item.orderNo }}</div>
                <div class="detail-comment" v-if="item.comment">{{ item.comment }}</div>
              </template>
            </div>
          </div>
          <div class="detail-right">
            <div class="detail-points" :class="{'text-success': item.type === 1, 'text-danger': item.type === 0 || item.type === 2}">
              {{ item.type === 1 ? '+' : '-' }}{{ item.points }}
            </div>
            <div class="detail-time">{{ parseTime(item.createTime) }}</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPointsByUserId, tradePagePay, order } from "@/api/points/points";
import store from "@/store";

export default {
  name: "Points",
  data() {
    return {
      userInfo: {
        userName: '',
        avatar: '',
        email: '',
        phoneNumber: '',
        points: 0
      },
      // 充值相关
      rechargeOpen: false,
      rechargeForm: {
        totalAmount: 100
      },
      rechargeRules: {
        totalAmount: [
          { required: true, message: '请选择充值金额', trigger: 'change' }
        ]
      },
      rechargeLoading: false,
      detailsVisible: false,
      pointsDetails: [],
    };
  },
  computed: {
    userId() {
      return store.state.user.id;
    }
  },
  created() {
    this.getUserPoints();
  },
  methods: {
    /** 获取用户积分信息 */
    getUserPoints() {
      getPointsByUserId(this.userId).then(response => {
        if (response.code === 200) {
          this.userInfo = response.data;
        }
      });
    },
    /** 充值按钮操作 */
    handleRecharge() {
      if (!this.userId) {
        this.$message.warning('请先登录');
        return;
      }
      this.rechargeOpen = true;
    },
    /** 取消充值 */
    cancelRecharge() {
      this.rechargeOpen = false;
      this.rechargeForm.totalAmount = 100;
    },
    /** 提交充值 */
    submitRecharge() {
      this.$refs.rechargeForm.validate(valid => {
        if (valid) {
          this.rechargeLoading = true;
          const payData = {
            userId: this.userId,
            totalAmount: this.rechargeForm.totalAmount
          };
          
          tradePagePay(payData).then(response => {
            this.rechargeLoading = false;
            if (response.code === 200) {
              // 创建支付表单并自动提交
              this.createPayForm(response.msg);
          } else {
              this.$message.error(response.msg || '充值失败');
            }
          }).catch(error => {
            this.rechargeLoading = false;
            console.error('充值失败:', error);
            this.$message.error('充值失败，请稍后重试');
          });
        }
      });
    },
    /** 创建支付表单并自动提交 */
    createPayForm(formHtml) {
      // 直接使用返回的表单内容
      document.write(formHtml);
      
      // 关闭充值对话框
      this.rechargeOpen = false;
    },
    /** 查看积分详情 */
    handleViewDetails() {
      this.detailsVisible = true;
      order(this.userId).then(response => {
        if (response.code === 200) {
          this.pointsDetails = response.data;
        }
      });
    },
    /** 获取类型标签样式 */
    getTypeTag(type) {
      switch (type) {
        case 0:
          return 'danger';
        case 1:
          return 'success';
        case 2:
          return 'warning';
        default:
          return 'info';
      }
    },
    /** 获取类型文本 */
    getTypeText(type) {
      switch (type) {
        case 0:
          return '订阅';
        case 1:
          return '充值';
        case 2:
          return '打赏';
        default:
          return '未知';
      }
    },
    /** 解析时间 */
    parseTime(timestamp) {
      const date = new Date(timestamp);
      return date.toLocaleString();
    }
  }
};
</script>

<style scoped>
.points-container {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 20px;
  min-height: calc(100vh - 200px);
  display: flex;
  align-items: center;
}

.user-info-section {
  display: flex;
  gap: 30px;
  margin-bottom: 30px;
  width: 100%;
}

.user-info-card {
  flex: 2;
  background: linear-gradient(135deg, #409EFF 0%, #36D1DC 100%);
  border-radius: 16px;
  padding: 60px;
  color: white;
  box-shadow: 0 10px 20px rgba(0,0,0,0.1);
  height: 400px;
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  width: 100%;
}

.avatar-container {
  margin-right: 50px;
}

.avatar-container .el-avatar {
  border: 4px solid rgba(255, 255, 255, 0.3);
}

.info-container {
  flex: 1;
}

.username {
  font-size: 32px;
  margin: 0 0 30px 0;
  color: white;
  font-weight: 600;
}

.info-item {
  display: flex;
  align-items: center;
  margin: 20px 0;
  font-size: 18px;
  color: rgba(255, 255, 255, 0.9);
}

.info-item i {
  margin-right: 15px;
  font-size: 24px;
}

.points-card {
  flex: 1;
  background: white;
  border-radius: 16px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 10px 20px rgba(0,0,0,0.1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 400px;
}

.points-header {
  font-size: 24px;
  color: #606266;
  margin-bottom: 30px;
}

.points-header i {
  margin-right: 10px;
  color: #409EFF;
  font-size: 28px;
}

.points-value {
  font-size: 64px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 20px;
  line-height: 1;
}

.points-desc {
  font-size: 16px;
  color: #909399;
  margin-bottom: 40px;
}

.points-actions {
  display: flex;
  flex-direction: column;
  gap: 15px;
  width: 100%;
}

.points-actions .el-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  border-radius: 25px;
  margin: 0;
}

.points-actions .el-button + .el-button {
  margin-left: 0;
}

.amount-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  padding: 30px 0;
}

.el-radio-button {
  margin: 0;
}

.el-radio-button__inner {
  width: 100%;
  padding: 20px 0;
  font-size: 18px;
  border-radius: 8px;
  transition: all 0.3s;
}

.el-radio-button__inner:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.dialog-footer {
  text-align: center;
  padding-top: 30px;
}

.dialog-footer .el-button {
  padding: 15px 40px;
  font-size: 18px;
  border-radius: 25px;
}

.recharge-dialog {
  border-radius: 16px;
}

.recharge-dialog .el-dialog__header {
  padding: 30px 20px;
  border-bottom: 1px solid #EBEEF5;
}

.recharge-dialog .el-dialog__title {
  font-size: 24px;
  font-weight: 600;
}

.recharge-dialog .el-dialog__body {
  padding: 40px 20px;
}

.points-details {
  padding: 20px;
}

.points-detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-bottom: 15px;
  background: #f8f9fa;
  border-radius: 12px;
  transition: all 0.3s;
}

.points-detail-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.detail-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.detail-type {
  min-width: 60px;
}

.detail-info {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.detail-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
}

.detail-subtitle {
  font-size: 14px;
  color: #909399;
}

.detail-comment {
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
  line-height: 1.4;
  text-align: left;
}

.detail-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
}

.detail-points {
  font-size: 18px;
  font-weight: 600;
}

.detail-time {
  font-size: 14px;
  color: #909399;
}

.text-success {
  color: #67C23A;
}

.text-danger {
  color: #F56C6C;
}
</style>
